package dwei.core.kv
{
	import dwei.core.util.StringUtil;

	internal dynamic class KVLibrary
	{
		public function KVLibrary(property:String)
		{
			if (property == null) return;
			parse(property);
		}
		
		private function parse(config:String):void
		{
			config = config.replace(/--.*/g, "");//删除行注释
			config = config.replace(/\r/g, "").replace(/;/g, "\n").replace(/\n+/g, "\n");//格式化换行标识
			var kvsUndressed:Array = config.split(/\n/);//按行分组
			
			var index:int;
			var kv:String;
			var kvs:Array = [];
			for (index = kvsUndressed.length - 1;index >= 0;index--)
			{
				kv = kvsUndressed[index];
				kv = StringUtil.strim(kv);
				if (isKeyValue(kv)) kvs.push(kv);
			}
			
			var units:Array;
			var k:String;
			var v:String;
			for (index = kvs.length - 1;index >= 0;index--)
			{
				kv = kvs[index];
				units = kv.split("=");
				k = StringUtil.strim(units[0]);
				v = StringUtil.strim(units[1]);
				if (v != "") this[k] = v;
			}
		}
		
		private function isKeyValue(value:String):Boolean
		{
			var regKV:RegExp = /^\s*\w+\s*=.*$/;
			return regKV.test(value);
		}
	}
}